package General;
import java.io.Serializable;
import java.util.StringTokenizer;


import Gui.status_panel;


public class Phrase implements Serializable{

	private static final long serialVersionUID = -8171884448081648874L;
	String def;
	String exp;
	StringBuffer blanks;
	boolean[] alreadyAsked = new boolean[26];
	String hint = "";
	private String id ="";
	String blurb = "";


	public Phrase(String def,  String exp) {
		this.def = def;
		this.exp = exp;
		this.blanks = new StringBuffer(exp.length());
		for (int i=0; i<exp.length(); i++) {
			if ((exp.charAt(i) == ' '))
				blanks.append(' ');
			else if ((exp.charAt(i) == '-')) 
				blanks.append('-');
			else		
				blanks.append('#');
		}
	}
	
	public void setBlurb(String content){
		this.blurb = content;
	}
	
	public Phrase(String def,  String id , String exp, String hints) {
		initPhrase(def, exp, hints);
		this.id = id;
	}
	public void initPhrase(String def, String exp, String hints) {
		this.def = def;
		this.exp = exp;
		this.hint = hints;
		this.blanks = new StringBuffer(exp.length());
		for (int i=0; i<exp.length(); i++) {
			if ((exp.charAt(i) == ' '))
				blanks.append(' ');
			else if ((exp.charAt(i) == '-')) 
				blanks.append('-');
			else		
				blanks.append('#');
		}
	}


	public int guessLet(char letter) {     /*returns num of occurrences*/
		int shows = 0;
		for (int i=0; i<exp.length(); i++) {
			if ((exp.toUpperCase().charAt(i) == letter) || (exp.toLowerCase().charAt(i) == letter)) {
				shows++;
				blanks.setCharAt(i, exp.charAt(i));
			}
		}
		return shows;
	}

	public boolean guessExp(String guess) {     /*returns num of occurrences*/
		if ((exp.equalsIgnoreCase(guess)) || (exp.replace("-", " ").equalsIgnoreCase(guess))) {
			blanks = new StringBuffer(exp);
			return true;
		}
		return false;
	}

	public void getStatus() {
		status_panel.println(blanks.toString());
		String st = this.blanks.toString();
		st = st.replace('-', ' ');
		StringTokenizer st1 = new StringTokenizer(st);
		status_panel.println(def+" ( ");
		while (st1.hasMoreTokens()) {
			status_panel.print(st1.nextToken().length()+" ");
		}
		status_panel.print(")");

	}


	public boolean Guessed() {
		if (blanks.lastIndexOf("#") == -1)
			return true;
		return false;
	}

	public boolean checkAsked(char letter) {
		letter = Character.toUpperCase(letter);
		if (alreadyAsked[((int)letter)-65] == true) {
			return true; 
		}
		alreadyAsked[((int)letter)-65] = true;
		return false;
	}

	public String getExp(){
		return exp;
	}

	public String getDef(){
		return def;
	}
		public StringBuffer getBlanks() {
		return blanks;		
	}
	
	public boolean isHintExist(){
		return (hint!=null && !hint.isEmpty());
	}
	
	public String getHint(){
		if (isHintExist()){
			return hint;
		}
		return "";
	}
	
	public boolean isBlurbExist(){
		return (blurb!=null && !blurb.isEmpty());
	}
	
	public String getBlurb(){
		if (isBlurbExist()){
			return blurb;
		}
		return "";
	}
	
	public String getID(){
		return id;
	}
	public String getId() {
		return id;
	}

}

